package com.cskaoyan.order.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.mall.commons.exception.BizException;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.order.dto.OrderDetailResponse;
import com.cskaoyan.order.dto.OrderListResponse;
import com.cskaoyan.order.form.CancelOrderRequest;
import com.cskaoyan.order.form.CreateOrderRequest;
import com.cskaoyan.order.form.OrderListRequest;
import com.cskaoyan.order.service.OrderCoreService;
import com.cskaoyan.order.service.OrderQueryService;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.UUID;

/**
 * @Description:
 * @since: 2022-10-25 17:35
 * @author: lazy
 */

@RestController
@RequestMapping("/shopping")
public class OrderController {

    @Autowired
    private OrderCoreService orderCoreService;


    @Autowired
    private OrderQueryService orderQueryService;

    @PostMapping("/order")
    public ResponseData createOrder(@RequestBody CreateOrderRequest request, HttpServletRequest servletRequest) {
        //{"uid":1, "username":"zs"}
        String userInfo = servletRequest.getHeader("user_info");
        JSONObject object = JSON.parseObject(userInfo);
        //获取ID
        long uid = Long.parseLong(object.get("uid").toString());
        request.setUserId(uid);

        //设置uniqueKey
        request.setUniqueKey(UUID.randomUUID().toString());
        String order = null;

        //try {
            order = orderCoreService.createOrder(request);
        // } catch (Exception e) {
        //     e.printStackTrace();
        //     throw new BizException(e);
        // }
        return new ResponseData(true, "success", 200, order, new Date().getTime());

    }


    @GetMapping("/order")
    public ResponseData selectAllOrders(@RequestParam Integer page,Integer size, HttpServletRequest servletRequest) {
        //{"uid":1, "username":"zs"}
        String userInfo = servletRequest.getHeader("user_info");
        JSONObject object = JSON.parseObject(userInfo);
        //获取ID
        OrderListRequest pageInfo = new OrderListRequest();
        pageInfo.setPage(page);
        pageInfo.setSize(size);
        long uid = Long.parseLong(object.get("uid").toString());
        pageInfo.setUserId(uid);
        OrderListResponse orderListResponse = orderQueryService.queryAllOrdersByUid(pageInfo);

        return new ResponseData(true,"success",200,orderListResponse,new Date().getTime());

    }


    @GetMapping("/order/{id}")
    public ResponseData orderDetail(@PathVariable String id) {

        OrderDetailResponse orderDetailResponse = orderQueryService.orderDetail(id);
        return new ResponseData(true, "success", 200, orderDetailResponse, new Date().getTime());

    }


    @PostMapping("/cancelOrder")
    public ResponseData cancelOrder(@RequestBody CancelOrderRequest request,HttpServletRequest servletRequest) {
        orderCoreService.cancelOrder(request.getOrderId());

        return new ResponseData(true, "success", 200,"成功",new Date().getTime());

    }

    @DeleteMapping("/order/{id}")
    public ResponseData deleteOrder(@PathVariable("id") String id) {

       orderCoreService.deleteOrder(id);
        return new ResponseData(true, "success", 200,"成功",new Date().getTime());

    }


}
